import getpath
import os
import openpyxl
import re
import logging
def elementWave(s):
    print(s)
    find=re.search(r"[A-Z][a-z]*\d+(\.\d+)?", s).group()
    wave=re.search(r"\d+(\.\d+)?", find).group()
    element=re.search(r"[A-Z][a-z]*", find).group()
    return(element,wave)
def getdata_raw(filename):
    from openpyxl import load_workbook
    xl_book = load_workbook(filename=filename)
    table = xl_book.worksheets[0]
    data = []
    for row in table.values:
        r = []
        for one in row:
            if one is not None:
                r.append(one)
            else:
                r.append("")
        data.append(r)
    fn=os.path.split(filename)[1]
    id1=os.path.splitext(fn)[0]
    return (id1,data)
def zl_data(id1,data):
    data=data[3:]
    sample={"id":id1}
    items=[]
    concs=[]
    eleBegin=False
    for i in range(len(data)):
        row=data[i]
        if row[0]=="":
            break
        items.append(elementWave(row[0]))
        concs.append(row[6])
    sample["items"]=items
    sample["concs"]=concs
    return[sample,]
def getdata(fname):
    (id1,data0)=getdata_raw(fname)
    data=zl_data(id1,data0)
    return data
def test_getdata():
    fname=os.path.join(getpath.getpath(),"thermo_gdms.xlsx")
    data=getdata(fname)
    # print(data)
    return data
if __name__=="__main__":
    data=test_getdata()
    print(data)
    # for one in data:
    #     print(one["id"])
    # print(data[0])
    # print(data[1])
    # print(data[-2])
    # print(data[-1])